package hu.mapro.model.roo;

import static org.springframework.roo.project.Path.SRC_MAIN_JAVA;

import java.util.ArrayList;
import java.util.List;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.springframework.roo.addon.gwt.GwtOperations;
import org.springframework.roo.classpath.PhysicalTypeIdentifier;
import org.springframework.roo.classpath.TypeManagementService;
import org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder;
import org.springframework.roo.model.JavaType;
import org.springframework.roo.project.Dependency;
import org.springframework.roo.project.DependencyScope;
import org.springframework.roo.project.DependencyType;
import org.springframework.roo.project.LogicalPath;
import org.springframework.roo.project.ProjectOperations;

/**
 * Implementation of operations this add-on offers.
 *
 * @since 1.1
 */
@Component // Use these Apache Felix annotations to register your commands class in the Roo container
@Service
public class MaproModelOperationsImpl implements MaproModelOperations {
    
    @Reference private ProjectOperations projectOperations;
    @Reference protected GwtOperations gwtOperations;
    
    
    /**
     * Use TypeManagementService to change types
     */
    @Reference private TypeManagementService typeManagementService;

    List<Dependency> dependencies = new ArrayList<Dependency>();
    
    {
    	dependencies.add(new Dependency("hu.mapro", "maprohu-model-roo-annotation", "1.0", DependencyType.JAR, DependencyScope.PROVIDED));
    	dependencies.add(new Dependency("hu.mapro", "maprohu-model", "0.0.1-SNAPSHOT", DependencyType.JAR, DependencyScope.COMPILE));
    }
    
    
    
    /** {@inheritDoc} */
    public boolean isSetupCommandAvailable() {
        // Check if a project has been created
        return projectOperations.isFocusedProjectAvailable() && 
        		!projectOperations.getFocusedProjectMetadata().getPom().isAllDependenciesRegistered(dependencies);
    }

    private boolean isSetupComplete() {
        return projectOperations.isFocusedProjectAvailable() && 
        		projectOperations.getFocusedProjectMetadata().getPom().isAllDependenciesRegistered(dependencies);
    }
    
    
    /** {@inheritDoc} */
    public void setup() {
        // Install the add-on Google code repository needed to get the annotation 
        //projectOperations.addRepository("", new Repository("Roo Roo add-on repository", "Roo Roo add-on repository", "https://hu-mapro-model-roo.googlecode.com/svn/repo"));
        
        //List<Dependency> dependencies = new ArrayList<Dependency>();
        
        // Install the dependency on the add-on jar (
		//dependencies.add(dependency);
        
        // Install dependencies defined in external XML file
//        for (Element dependencyElement : XmlUtils.findElements("/configuration/batch/dependencies/dependency", XmlUtils.getConfiguration(getClass()))) {
//            dependencies.add(new Dependency(dependencyElement));
//        }

        // Add all new dependencies to pom.xml
        projectOperations.addDependencies("", dependencies);
    }

	public boolean isProxyCommandAvailable() {
		return isSetupComplete() && gwtOperations.isScaffoldAvailable();
	}

	public void proxy(JavaType proxyType) {
        final String focusedModule = projectOperations.getFocusedModuleName();
        final LogicalPath proxyLogicalPath = LogicalPath.getInstance(
                SRC_MAIN_JAVA, focusedModule);
        final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(
                PhysicalTypeIdentifier.createIdentifier(proxyType,
                        proxyLogicalPath));
        MaproModelUtil.initProxy(cidBuilder);
        cidBuilder.setName(proxyType);
        typeManagementService.createOrUpdateTypeOnDisk(cidBuilder.build());
    }
	
}